//
//  TreeTableViewController.swift
//  SwiftDemo
//
//  Created by John on 16/1/14.
//  Copyright © 2016年 SteamCat_343. All rights reserved.
//

import UIKit

var cellDescriptors: NSMutableArray!

class TreeTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    var dataArray =  [CellDescriptorModel]()
    var listTable: UITableView! = UITableView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height), style: UITableViewStyle.Grouped)
    let cellIdentifier = "CellIdentifier"
    var visibleRows = [Int]()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.whiteColor()
        self.title = "树状列表"
        self.view.addSubview(listTable)
        
        configureTableView()
        loadCellDescriptors()
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
    }
    
    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        
        
    }
    
    func loadCellDescriptors() {
        for var index = 0; index < 4; index++ {
            let cellDataS1: CellDescriptorModel! = CellDescriptorModel(isExpandable: true, isExpand: false,  isVisible: true, value: "adfadf", beSelected: false, primaryTitle: "adfadfas", secondaryTitle: "madsid", cellIdentifier: "nomalCell", additionalRows: 2)
            dataArray.append(cellDataS1)
            
            let cellDataS2: CellDescriptorModel! = CellDescriptorModel(isExpandable: false, isExpand: false, isVisible: false, value: "amvdis", beSelected: false, primaryTitle: "amvdis", secondaryTitle: "nibuhao", cellIdentifier: "expandCell", additionalRows: 0)
            dataArray.append(cellDataS2)
            
            let cellDataS3: CellDescriptorModel! = CellDescriptorModel(isExpandable: false, isExpand: false, isVisible: false, value: "amvdis", beSelected: false, primaryTitle: "amvdis", secondaryTitle: "nihao", cellIdentifier: "expandCell", additionalRows: 0)
            dataArray.append(cellDataS3)
            
        }
        getIndicesOfVisibleRows()
        listTable.reloadData()
    }
    
    func configureTableView() {
        listTable.delegate = self
        listTable.dataSource = self
        listTable.tableFooterView = UIView(frame: CGRectMake(0, 0, 0, 0))
        listTable.registerClass(UITableViewCell.self as AnyClass, forCellReuseIdentifier: cellIdentifier)
    }
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.visibleRows.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let currentCellData = getCellDescriptorForIndexPath(indexPath)
        let cell: UITableViewCell! = tableView.dequeueReusableCellWithIdentifier(cellIdentifier)
        if currentCellData.cellIdentifier as String == "nomalCell" {
            cell.textLabel?.text = currentCellData.primaryTitle
            cell.textLabel?.textColor = UIColor.blackColor()

        } else if currentCellData.cellIdentifier as String == "expandCell" {
            cell.textLabel?.text = currentCellData.secondaryTitle
            cell.textLabel?.textColor = UIColor.redColor()
        }
        if currentCellData.beSelected == true {
            cell.accessoryType = UITableViewCellAccessoryType.Checkmark
        } else {
            cell.accessoryType = UITableViewCellAccessoryType.None
        }
        return cell
    }
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        let cellData: CellDescriptorModel! = dataArray[indexPath.row]
        switch cellData.cellIdentifier as String {
        case "nomalCell":
            return 60
        default:
            return 44
        }
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//        let cell: UITableViewCell! = tableView.cellForRowAtIndexPath(indexPath)
//        
//        let cellData:CellDescriptorModel! = dataArray[indexPath.row]
//        if cellData.cellIdentifier == "expandCell" {
//            if cellData.beSelected == true {
//                cell.accessoryType = UITableViewCellAccessoryType.None
//                cellData.beSelected = !cellData.beSelected
//            } else {
//                cell.accessoryType = UITableViewCellAccessoryType.Checkmark
//                cellData.beSelected = !cellData.beSelected
//            }
//        }
       
//        let cell: UITableViewCell! = tableView.cellForRowAtIndexPath(indexPath)
        let indexOfTappedRow = visibleRows[indexPath.row]
        if dataArray[indexOfTappedRow].isExpandable as Bool == true {
            var shouldExpandAndShowSubRows = false
            if dataArray[indexOfTappedRow].isExpand as Bool == false {
                shouldExpandAndShowSubRows = true
            }
            dataArray[indexOfTappedRow].isExpand = shouldExpandAndShowSubRows
            
            for i in (indexOfTappedRow + 1)...(indexOfTappedRow + (dataArray[indexOfTappedRow].additionalRows as Int)){
                dataArray[i].isVisible = shouldExpandAndShowSubRows
            }
        }
        else {
            if dataArray[indexOfTappedRow].cellIdentifier as String == "expandCell" {
                var indexOfParentCell: Int!
                for var i = indexOfTappedRow - 1; i >= 0; --i {
                    if dataArray[i].isExpandable as Bool == true {
                        indexOfParentCell = i
                        break
                    }
                }
                
                dataArray[indexOfParentCell].isExpand = false
                for i in (indexOfParentCell + 1)...(indexOfParentCell + (dataArray[indexOfParentCell].additionalRows as Int)) {
                    dataArray[i].isVisible = false
                }
                
//                let cell: UITableViewCell! = tableView.cellForRowAtIndexPath(indexPath)
//                dataArray[indexOfParentCell].beSelected = !dataArray[indexOfParentCell].beSelected
//                if dataArray[indexOfParentCell].beSelected == true {
//                    cell.accessoryType = UITableViewCellAccessoryType.None
//                } else {
//                    cell.accessoryType = UITableViewCellAccessoryType.Checkmark
//                }

            }
        }
        
        getIndicesOfVisibleRows()
        listTable.reloadSections(NSIndexSet(index: 0), withRowAnimation: UITableViewRowAnimation.Fade)
    }
 
    func getIndicesOfVisibleRows(){
        visibleRows.removeAll()
        for var index = 0; index < dataArray.count; ++index {
            if dataArray[index].isVisible == true {
                visibleRows.append(index)
            }
        }
    }
    
    func getCellDescriptorForIndexPath(indexPath: NSIndexPath) -> CellDescriptorModel {
        let indexOfVisibleRow = visibleRows[indexPath.row]
        let cellData: CellDescriptorModel! = dataArray[indexOfVisibleRow]
        return cellData
    }

}
